﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Colors : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public List<Color> InnerColors { get; set; }

        public Colors()
        {
            this.InnerColors = new List<Color>();
        }

        public Colors(XmlNode colorsNode):this()
        {
            try
            {
                foreach (XmlNode colorNode in colorsNode.SelectNodes("Color"))
                {
                    if (colorNode.NodeType == XmlNodeType.Comment)
                        return;

                    Color color = new Color(colorNode);
                    this.InnerColors.Add(color);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode colorPickerNode)
        {
            try
            {
                XmlNode colorsXmlNode = XmlHelper.AddNode(colorPickerNode, "Colors");

                foreach (Color color in this.InnerColors)
                {
                    color.AddNode(colorsXmlNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode colorPickerNode)
        {
            try
            {
                TreeNode node = new TreeNode("Colors");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "Color" }, true);

                foreach (Color color in this.InnerColors)
                {
                    color.BuildTreeViewElement(node);
                }

                colorPickerNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is Color))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.InnerColors == null)
                    {
                        this.InnerColors = new List<Color>();
                    }

                    this.InnerColors.Add((Color)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is Color)
                {
                    this.InnerColors.Remove((Color)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
